<?php
$project = elgg_extract ( 'entity', $vars, FALSE );
$selectable = elgg_extract ( 'selectable', $vars, FALSE );
$submitlink = elgg_extract ( 'submitlink', $vars, FALSE );
$hidden_register = elgg_extract ( 'hidden_register', $vars, FALSE );
$deadline_edited = elgg_extract ( 'deadline_edited', $vars, "0" );

$student_project = elgg_extract ( 'student_project', $vars, FALSE );

$current_user = elgg_get_logged_in_user_entity ();

if ($project->group_url) {
	$project_name = elgg_view ( "output/url", array (
			'href' => $project->group_url,
			'text' => $project->title 
	) );
} else {
	$project_name = $project->title;
}

$base_url = elgg_get_site_url () . "submana/project/{$project->container_guid}";

$action_button = "elgg-button elgg-button-action";
$cancel_button = "elgg-button elgg-button-cancel";
$group = get_entity ( $project->container_guid );
$canEdit = $group->canEdit ( $current_user->guid );
$joined_member = false;
if (isset ( $project->students )) {
	
	// add list group member
	$html_students = "<div>" . elgg_echo ( 'submana:project:groupmembers' ) . "</div>";
	$html_students .= "<div class = 'margin_left_15'>";
	$html_students .= "<table class='table_none_border'>";
	$register_owner = true;
	
	$students = $project->students;
	
	if (is_array ( $students )) {
		foreach ( $students as $student_guid ) {
			$student = get_user_by_username ( $student_guid );
			// $student = get_entity ( $student_guid );
			
			if ($student) {
				if ($student->guid === elgg_get_logged_in_user_guid ()) {
					$joined_member = true;
				}
				if ($register_owner == true) {
					$register_owner = false;
					$red = "color:blue;";
				} else {
					$red = "";
				}
				
				$html_students .= "<tr><td><a href ='{$student->getURL()}'><span>{$student->name}</span></a></td><td><span style = '$red'>$student->username</span></td></tr>";
			}
		}
	} else {
		$student = get_user_by_username ( $students );
		// $student = get_entity ( $student_guid );
		
		if ($student) {
			if ($student->guid === elgg_get_logged_in_user_guid ()) {
				$joined_member = true;
			}
			if ($register_owner == true) {
				$register_owner = false;
				$red = "color:blue;";
			} else {
				$red = "";
			}
			
			$html_students .= "<tr><td><a href ='{$student->getURL()}'><span>{$student->name}</span></a></td><td><span style = '$red'>$student->username</span></td></tr>";
		}
	}
	
	$html_students .= "</table>";
	$html_students .= "</div>";
	if ($canEdit) {
		if ($project->approved) {
			$assign_url = "$base_url/assign/{$project->getGUID()}";
			$assign_link = elgg_view ( 'output/url', array (
					'href' => $assign_url,
					'text' => elgg_echo ( 'submana:assign' ),
					'class' => $action_button,
					'is_action' => true,
					'is_trusted' => true 
			) ) . "&nbsp &nbsp";
			
			$url = elgg_get_site_url () . "groups/mark/{$project->group_guid}";
			$url = elgg_add_action_tokens_to_url ( $url );
			$assign_link .= elgg_view ( 'output/url', array (
					'href' => $url,
					'text' => "Cho điểm",
					'class' => $action_button,
					'is_action' => true,
					'is_trusted' => true 
			) ) . "&nbsp &nbsp";
			
			
		} else {
			if (! $project->wait_project) {
				$approve_url = "$base_url/approve/{$project->getGUID()}";
				$approve_link = elgg_view ( 'output/url', array (
						'href' => $approve_url,
						'text' => elgg_echo ( 'submana:approval' ),
						'class' => $action_button,
						'is_action' => true,
						'is_trusted' => true 
				) ) . "&nbsp &nbsp";
				
				$reject_url = "$base_url/reject/{$project->getGUID()}";
				$reject_link = elgg_view ( 'output/url', array (
						'href' => $reject_url,
						'text' => elgg_echo ( 'submana:reject' ),
						'class' => $cancel_button,
						'is_action' => true,
						'is_trusted' => true 
				) ) . "&nbsp &nbsp";
			}
		}
	}
}

if ($current_user->usertype == "Student" && ($project->current_group < $project->max_group) && ! $hidden_register) {
	// get deadline of current project
	
	$deadline_date_str = explode ( "/", $project->deadline );
	$deadline = mktime ( 23, 59, 0, $deadline_date_str [1], $deadline_date_str [0], $deadline_date_str [2] );
	$currentdate = strtotime ( "now" );
	
	// compare to current date
	if ($currentdate <= $deadline) {
		
		$register_url = "$base_url/register/{$project->getGUID()}";
		$register_link = elgg_view ( 'output/url', array (
				'href' => $register_url,
				'text' => elgg_echo ( 'submana:register' ),
				'class' => $action_button,
				'is_action' => true,
				'is_trusted' => true 
		) );
	} else {
		$register_url = "#";
		$register_link = elgg_view ( 'output/url', array (
				'href' => $register_url,
				'text' => "Đã hết hạn đăng ký",
				'class' => $cancel_button,
				'is_action' => false,
				'is_trusted' => false 
		) );
	}
}

if ($project->approved) {
	$assiglistview = elgg_list_entities ( array (
			'type' => "object",
			'subtype' => "assignment",
			'container_guid' => $project->guid,
			'limit' => 1,
			'full_view' => false,
			'pagination' => false,
			'submitlink' => $submitlink 
	) );
	if (strlen ( $assiglistview ) < 70) {
	} else {
		$assign_all_url = "$base_url/assignall/{$project->getGUID()}";
		$assig_all = elgg_view ( 'output/url', array (
				'href' => $assign_all_url,
				'text' => elgg_echo ( 'submana:assignall' ),
				'is_action' => true,
				'is_trusted' => true 
		) ) . "&nbsp &nbsp";
		$assiglistview .= "<div class='right'>$assig_all</div>";
	}
}
if ($canEdit && $project->state != STATE_CLOSE) {
	$del_url = "$base_url/del/{$project->getGUID()}";
	$del_url = elgg_add_action_tokens_to_url ( $del_url );
	$del_confirm = elgg_echo ( 'submana:delete' );
	
	$edit_url = "$base_url/edit/{$project->getGUID()}";
	$edit_url = elgg_add_action_tokens_to_url ( $edit_url );
	$edit_label = elgg_echo ( 'submana:edit' );
	
	$edit_del_url = "<div class='right'>
	<ul	class='elgg-menu elgg-menu-entity elgg-menu-hz elgg-menu-entity-default'>
	<li class='elgg-menu-item-edit'><a href='$edit_url'>$edit_label</a></li>
	<li class='elgg-menu-item-delete'><a href='$del_url'
	rel='$del_confirm'
	class='elgg-requires-confirmation'>Xóa</a></li>
	</ul>
	</div>";
}
if ($selectable) {
	$checkbox = elgg_view ( 'input/checkbox', array (
			'name' => "check_list[]",
			'default' => false,
			'value' => $project->getGUID (),
			'id' => "checkbox_{$project->getGUID()}" 
	) );
}
if (isset ( $project->rate )) {
	// $rate = elgg_view ( 'output/star', array (
	// 'checked' => $project->rate
	// ) );
}

if ($project->wait_project) {
	$divclass = "wait-project margin-10 padding-10";
}

// --------------------------
if ($project->wait_project) {
	if ($joined_member) {
		
		$register_url = "$base_url/outregister/{$project->getGUID()}";
		$register_link = elgg_view ( 'output/url', array (
				'href' => $register_url,
				'text' => elgg_echo ( "Rút khỏi" ),
				'class' => $action_button,
				'is_action' => true,
				'is_trusted' => true 
		) );
	}
}

// --------------------------

// show mark
if ($student_project) {
	$tearcher_review_url = elgg_view ( 'output/url', array (
			'text' => "Đánh giá của giảng viên",
			'href' => "#",
			'onclick' => "toggle('mark-$project->guid'); return false;" 
	) );
	$marks = $tearcher_review_url;
	$marks .= "<div id='mark-$project->guid' style='display:none;'>";
	
	$marks_view .= elgg_list_entities_from_relationship ( array (
			'relationship' => "mark",
			'relationship_guid' => $project->guid,
			'type' => "object",
			'subtype' => "mark" 
	) );
	
	$marks_view .= elgg_list_entities_from_relationship ( array (
			'relationship' => "review",
			'relationship_guid' => $project->guid,
			'type' => "object",
			'subtype' => "review"
	) );
	
	if (strlen ( $marks_view ) < 140) {
		$marks_view = "Chưa có đánh giá của giảng viên";
	}
	$marks .= $marks_view;
	
	$marks .= "</div>";
}

?>



<div class="<?php echo $divclass;?>">
    <?php
				echo $edit_del_url;
				?>
    <div class="left registered project_name">
        <?php
								echo $checkbox;
								echo $project_name;
								
								?>
    </div>
	<div class="clearboth"></div>
	<div style="margin-left: 20px;">

        <?php
								if ($deadline_edited == "1") {
									echo "<br />";
									echo "<b>Ngày hết hạn đăng ký: </b>";
									echo $project->deadline;
								}
								?>
        <br /> <i>
            <?php echo $project->description; ?>
        </i>

	</div>


	<div class="clearboth" style="clear: both;"></div>
	<div style="margin: 10px;">
        <?php
								echo $html_students;
								?>
        <div style="margin-left: 50px;">
            <?php
												echo $assiglistview;
												?>
        </div>
		</br>

		<div>
			<?php echo $marks;?>
			
		</div>
		<div>
            <?php
												echo $approve_link;
												echo $reject_link;
												echo $assign_link;
												echo $register_link;
												?>
        </div>
	</div>

</div>

<script type="text/javascript">
function toggle(project_group){
	$("#"+project_group).toggle();
}					
</script>
